package model

// by daoyi
import (
	"github.com/gin-gonic/gin"
	"simai_oa/tool"
)

type AdminLogs struct {
	Id        uint   `gorm:"primary_key"`
	CreatedAt string `gorm:"type:varchar(64);column:create_at;default:null" json:"created_at" form:"created_at"`
	UpdatedAt string `gorm:"type:varchar(64);column:update_at;default:null" json:"updated_at" form:"updated_at"`
	Type      uint   `gorm:"column:type;default:0;comment:动作类型 0登录 1添加 2修改 3删除" json:"type" form:"type"`
	ActType   string `gorm:"type:varchar(32);column:act_type;comment:动作类型名称 登录 添加 修改 删除" json:"act_type" form:"act_type"`
	RunUrl    string `gorm:"type:text;column:run_url;comment:执行链接" json:"run_url" form:"run_url"`
	RefUrl    string `gorm:"type:text;column:ref_url;comment:上一个请求" json:"ref_url" form:"ref_url"`
	Username  string `gorm:"type:varchar(64);column:username;comment:用户名" json:"username" form:"username"`
	Remark    string `gorm:"type:varchar(255);column:remark;comment:描述" json:"remark" form:"remark"`
	UserIP    string `gorm:"type:varchar(32);column:user_ip;comment:ip" json:"user_ip" form:"user_ip"`
}

func AdminLog(mold uint, remark string, c *gin.Context) {
	//获取token中存的值
	token, _ := tool.GetTokenFromRequest(c)
	name := tool.GetValueFromTokenClaims("Name", token.Claims)

	admin_log := AdminLogs{}
	types := []string{"登陆", "添加", "修改", "删除"}
	admin_log.CreatedAt = tool.GetDate()
	admin_log.UpdatedAt = tool.GetDate()
	admin_log.Type = mold
	admin_log.ActType = types[mold]
	admin_log.RunUrl = c.Request.RequestURI
	admin_log.Username = name
	admin_log.Remark = remark
	admin_log.UserIP = c.ClientIP()

	tool.Gdb.Create(&admin_log)
}
